package com.polaris.live.utils.zego.media

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.MediaMetadataRetriever
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

/**
 *
 * @author Created by 天晴 on 2024/9/19 10:05
 * @since 1.0.0
 **/
@Parcelize
data class MusicBean(
    val id: Long,
    val name: String,
    val path: String,
    val duration: Long,
    val artist: String,
    val albumArt: String?,
) : Parcelable {

    override fun toString(): String {
        return "MusicBean{" +
                "id='" + id + '\'' +
                "name='" + name + '\'' +
                ", artist='" + artist + '\'' +
                ", duration=" + duration +
                ", path='" + path + '\'' +
                '}'
    }

    fun getAlbumCoverFromMusicFile(): Bitmap? {
        val retriever = MediaMetadataRetriever()
        return try {
            retriever.setDataSource(path)
            val art = retriever.embeddedPicture // 获取嵌入的封面图像
            art?.let { BitmapFactory.decodeByteArray(it, 0, it.size) }
        } catch (e: Exception) {
            e.printStackTrace()
            null
        } finally {
            retriever.release()
        }
    }
}
